import Vue from 'vue'
import Router from 'vue-router'
import login from '@/views/login'
import Index from '@/views/Index'
import Home from '@/views/home'
import personal from '@/views/personal'
import ExamIdex from '@/views/examIndex'
import sroucIndex from '@/views/sroucIndex'
import sroucxq from '@/views/sroucxq'
import showError from '@/views/showError'
import ranking from '@/views/ranking'
import video from '@/views/video'
import setUp from '@/views/setUp'
import practice from '@/views/practice'
import book from '@/views/book'
import bookDetail from '@/views/bookDetail'
import read_book from '@/views/read_book'
import changeLogin from '@/views/changeLogin'
import book_search from '@/views/book_search'
import catalog from '@/views/catalog'

Vue.use(Router)

const router = new Router({
  //新增
  mode: 'hash',
  routes: [
    {
      path: '/',
      name: 'login',
      component: login
    },
    {
      path: '/index',
      name: 'Index',
      component: Index,
      meta: {
        is_show: true
      }
    },
    {
      path: '/book',
      name: 'book',
      component: book,
      meta: {
        is_show: true
      }
    },
    {
      path: '/video',
      name: 'Video',
      component: video,
      meta: {
        is_show: true
      }
    },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      meta: {
        is_show: true
      }
    },
    {
      path: '/personal',
      name: 'personal',
      component: personal,
    },
    {
      path: '/examIndex',
      name: 'ExamIndex',
      component: ExamIdex,
    },
    {
      path: '/sroucIndex',
      name: 'sroucIndex',
      component: sroucIndex,
    },
    {
      path: '/sroucxq',
      name: 'sroucxq',
      component: sroucxq,
    },
    {
      path: '/showError',
      name: 'showError',
      component: showError,
    },
    {
      path: '/ranking',
      name: 'ranking',
      component: ranking,
    },
    {
      path: '/setUp',
      name: 'SetUp',
      component: setUp,
    },
    {
      path: '/practice',
      name: 'practice',
      component: practice,
    },
    {
      path: '/bookDetail/:id',
      inject: ['reload'],
      name: 'bookDetail',
      component: bookDetail,
    },
    {
      path: '/read_book/:id/:chapterID',
      name: 'read_book',
      component: read_book,
    },
    {
      path: '/changeLogin',
      name: 'changeLogin',
      component: changeLogin,
    },
    {
      path: '/book_search',
      name: 'book_search',
      component: book_search,
    },
    {
      path: '/catalog/:book_id',
      name: 'catalog',
      component: catalog,
    }


  ]
});
router.beforeEach((to, from, next) => {
  var is_login = localStorage.getItem('id')

  if (to.path != '/' && is_login == null && to.path != '/changeLogin') {

    next('/')
  }

  if (to.path == '/' && is_login != null) {
    next(from.path)
  }
  if (to.path == '/changeLogin' && is_login != null) {
    next(from.path)
  }
  next()
})

export default router